package cn.com.nxt.txb;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.com.nxt.txb.fragment.MineFragment;
import cn.com.nxt.txb.httpdelegate.MeetingDelegate;
import cn.com.nxt.txb.util.AppUtil;

@ContentView(R.layout.activity_my_meeting)
public class MyMeetingActivity extends BaseActivity {
    @ViewInject(R.id.tv_room)
    private TextView mRoomTv;
    @ViewInject(R.id.tv_username)
    private TextView mUsernameTv;
    private final byte MEETING_INFO = 0x00;
    private final byte SET_MEETING_INFO = 0x01;
    private MeetingDelegate delegate;
    public static String meetingName;
    public static String roomid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
        delegate = new MeetingDelegate(this);
        //if(--meetingInfo_success--)
        if (TextUtils.isEmpty(meetingName))
            delegate.meetingInfo(AppUtil.mUser.getToken(), AppUtil.mUser.getRommid(), MEETING_INFO);
        mUsernameTv.setText(TextUtils.isEmpty(meetingName) ? AppUtil.mUser.getUsername() : meetingName);
        mRoomTv.setText(TextUtils.isEmpty(roomid) ? AppUtil.mUser.getRommid() : roomid);
    }

    @Event({R.id.ll_username, R.id.ll_set})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.ll_username:
                Intent intent = new Intent(MyMeetingActivity.this,
                        ModifyUsernameActivity.class);
                intent.putExtra("meeting", true);
                startActivityForResult(intent, UserInfoActivity.RESULT_NAME);
                break;
            case R.id.ll_set:
                set();
                break;
        }
    }

    private void set() {
        startActivity(new Intent(MyMeetingActivity.this, MeetingSetActivity.class));
    }

    @Override
    public void onActivityResult(int arg0, int arg1, Intent data) {
        super.onActivityResult(arg0, arg1, data);
        if (arg0 == UserInfoActivity.RESULT_NAME) {
            mUsernameTv.setText(meetingName);
            //mRoomTv.setText(roomid);
        }
    }

    @Override
    public void success(String result, byte flag) {
        super.success(result, flag);
        if (flag == MEETING_INFO || flag == SET_MEETING_INFO)
            meetingInfo(result);
    }

    private void meetingInfo(String result) {
        JSONObject obj = null;
        try {
            obj = new JSONObject(result);
            meetingName = obj.optString("name");
            roomid = obj.optString("roomid");
            mUsernameTv.setText(meetingName);
            mRoomTv.setText(roomid);
        } catch (JSONException e) {
        }
    }
}